﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LibrarySystem
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        private string _persID = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            _persID = Request.QueryString["persID"];

            BusinessLayer.Borrower thisBorrower = BusinessLayer.Borrower.getBorrowerByID(_persID);
            borrowerName.Text = thisBorrower.FirstName + " " + thisBorrower.LastName;
            borrowerID.Text = thisBorrower.PersonId;
            borrowerAddress.Text = thisBorrower.Address;
            borrowerTelNo.Text = thisBorrower.Telno;
            borrowerCategory.Text = "Kategori: " + thisBorrower.CategoryId.ToString();

            List<BusinessLayer.Loan> actList = BusinessLayer.Loan.getActiveLoans(_persID);
            adminBorrowerActiveLoans.DataSource = actList;
            adminBorrowerActiveLoans.DataBind();

            List<BusinessLayer.Loan> histList = BusinessLayer.Loan.getLoanHistory(_persID);
            adminBorrowerHistory.DataSource = histList;
            adminBorrowerHistory.DataBind();
        }

       
        protected void adminCtrlModify_Click(object sender, EventArgs e)
        {
            if (adminModify.Visible == false)
            {
                BusinessLayer.Borrower thisBorrower = new BusinessLayer.Borrower();
                thisBorrower = BusinessLayer.Borrower.getBorrowerByID(_persID);
                if (thisBorrower != null)
                {
                    modifyFirstName.Text = thisBorrower.FirstName;
                    modifyLastName.Text = thisBorrower.LastName;
                    modifyAddress.Text = thisBorrower.Address;
                    modifyTele.Text = thisBorrower.Telno;
                    modifyCategory.Text = thisBorrower.CategoryId.ToString();

                    adminModify.Visible = true;
                }
            }
            else
                adminModify.Visible = false;
        }

        protected void adminCtrlRemove_Click(object sender, EventArgs e)
        {
            BusinessLayer.Borrower.RemoveBorrower(_persID);
            Response.Redirect("adminSearch.aspx?val=bkTitle&term=");
        }

        protected void adminCtrlSave_Click(object sender, EventArgs e)
        {
            BusinessLayer.Borrower.ModBorrower(_persID, modifyFirstName.Text, modifyLastName.Text, modifyAddress.Text, modifyTele.Text, Convert.ToInt32(modifyCategory.Text));
            Response.Redirect(Request.RawUrl);
        }

        protected void extendLoanBtn_Click(object sender, CommandEventArgs e)
        {
            string barcode = e.CommandName.ToString();
            BusinessLayer.Loan.extendLoan(_persID, barcode);
            Response.Redirect(Request.RawUrl);
        }
    }
}